package com.hangwei.paperless.widget;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.paperless.R;
import com.hangwei.paperless.fragment.PicFragment;
import com.twelve.all.net.API;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

import me.relex.photodraweeview.OnPhotoTapListener;
import me.relex.photodraweeview.OnViewTapListener;
import me.relex.photodraweeview.PhotoDraweeView;

/**
 * Created by 张龙臣 on 2020/11/20.
 */

public class PicDialog extends DialogFragment {

    private int height;
    private int mWidth;
    private ViewPager mViewPager;
    private PicAdapter mAdapter;
    private int mPosition;

    public static PicDialog newInstance(ArrayList<String> list, int position) {
        PicDialog fragment = new PicDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("list", list);
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        height = (int) (Tools.getScreenSize()[0] * 0.6f);
        mWidth = Tools.getScreenSize()[1];//无效
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        View view = inflater.inflate(R.layout.pic_pop, null);
        ImageView back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mViewPager = view.findViewById(R.id.viewPager);
        ArrayList<String> list = getArguments().getStringArrayList("list");
        int position = getArguments().getInt("position");
        mAdapter = new PicAdapter(list, getChildFragmentManager()) {
            @Override
            protected void goNext(int position) {
                mViewPager.setCurrentItem(position + 1, true);
            }

            @Override
            protected void goPrevious(int position) {
                mViewPager.setCurrentItem(position - 1, true);
            }
        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(position, false);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
            // 一定要设置Background，如果不设置，window属性设置无效
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            DisplayMetrics dm = new DisplayMetrics();
            if (getActivity() != null) {
                WindowManager windowManager = getActivity().getWindowManager();
                if (windowManager != null) {
                    windowManager.getDefaultDisplay().getMetrics(dm);
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.gravity = Gravity.CENTER;
                    // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//                    params.horizontalMargin = Tools.dp2px(10);
//                    params.verticalMargin = Tools.dp2px(10);
                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    params.height = ViewGroup.LayoutParams.MATCH_PARENT;
                    window.setAttributes(params);
                }
            }
        }
    }

    public void setStatus(ArrayList<String> data, int position) {
        if (mAdapter != null) {
            mAdapter.setData(data);
            mPosition = position;
        }
    }

    abstract static class PicAdapter extends FragmentPagerAdapter {

        private List<String> mList;

        public PicAdapter(List<String> list, FragmentManager childFragmentManager) {
            super(childFragmentManager);
            mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Fragment getItem(final int position) {
            PicFragment picFragment = new PicFragment();
            picFragment.setViewInit(new PicFragment.ViewInit() {
                @Override
                public void initView(View inflate) {
                    ImageView ivLeft = inflate.findViewById(R.id.ivLeft);

                    ImageView ivRight = inflate.findViewById(R.id.ivRight);
                    PhotoDraweeView sdv = inflate.findViewById(R.id.sdv);
                    ivLeft.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
                    ivRight.setVisibility(position == mList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
                    sdv.setOnPhotoTapListener(new OnPhotoTapListener() {
                        @Override
                        public void onPhotoTap(View view, float x, float y) {

                        }
                    });
                    sdv.setOnViewTapListener(new OnViewTapListener() {
                        @Override
                        public void onViewTap(View view, float x, float y) {

                        }
                    });
                    sdv.setPhotoUri(Uri.parse(API.BASE_URL + mList.get(position)));
//                    sdv.setPhotoUri(Uri.parse("res:///"+R.drawable.server_pic_1));
                    ivLeft.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            goPrevious(position);
                        }
                    });
                    ivRight.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            goNext(position);
                        }
                    });
                }
            });
            return picFragment;
        }

        protected abstract void goNext(int position);

        protected abstract void goPrevious(int position);

        public void setData(ArrayList<String> data) {
            mList = data;
            notifyDataSetChanged();
        }
    }
}
